package com.csdj.t309springboot.utils;

import org.apache.tomcat.util.buf.HexUtils;
import org.springframework.util.Base64Utils;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class EncryptUtil {

    public static final String KEY_HMAC="HmacMD5";
    public static final String ALGO_AES = "AES";
    public static final String AES_MODEL = "AES/ECB/PKCS5Padding";
    public static final String AES_KEY = "AIEJFALHsiejfae2";

    public static String getMd5(String str){
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("md5");
            md5.update(str.getBytes());
            byte[] digest = md5.digest();
            return HexUtils.toHexString(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getSha256(String str){
        MessageDigest sha256 = null;
        try {
            sha256 = MessageDigest.getInstance("sha256");
            sha256.update(str.getBytes());
            byte[] digest = sha256.digest();
            return HexUtils.toHexString(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String initMacKey(){
        KeyGenerator instance = null;
        try {
            instance = KeyGenerator.getInstance(KEY_HMAC);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        SecretKey secretKey = instance.generateKey();
        return Base64.getEncoder().encodeToString(secretKey.getEncoded());
    }

    public static String encryByHmac(String data,String secret){
        SecretKey  secretKey = new SecretKeySpec(Base64.getDecoder().decode(secret), KEY_HMAC);
        try {
            Mac instance = Mac.getInstance(KEY_HMAC);
            instance.init(secretKey);
            byte[] bytes = instance.doFinal(data.getBytes());
            return Base64.getEncoder().encodeToString(bytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String encryptByAes(String data){
        Cipher instance = null;
        try {
            instance = Cipher.getInstance(AES_MODEL);
            SecretKey secretKey = new SecretKeySpec(AES_KEY.getBytes(), ALGO_AES);
            instance.init(Cipher.ENCRYPT_MODE,secretKey);
            byte[] bytes = instance.doFinal(data.getBytes());
            return Base64.getEncoder().encodeToString(bytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decryptByAes(String encrypted){
        Cipher instance = null;
        try {
            instance = Cipher.getInstance(AES_MODEL);
            SecretKey secretKey = new SecretKeySpec(AES_KEY.getBytes(), ALGO_AES);
            instance.init(Cipher.DECRYPT_MODE,secretKey);
//            instance.
            byte[] bytes = instance.doFinal(Base64.getDecoder().decode(encrypted));
            return new String(bytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }






    public static void main(String[] args) throws IOException {
        String de = encryptByAes("abcdeadfasdfaseasvzxefzscvdz");
        System.out.println(de);
        System.out.println(decryptByAes(de));
    }
}
